﻿using System.Web.Mvc;
using MvcContrib;
using Wayloop.Blog.Core.Data;
using Wayloop.Blog.Core.Data.Entities;


namespace Wayloop.Blog.Presentation.Controllers.Admin
{
    [Authorize]
    public class CommentController : AbstractController
    {
        private const int PageSize = 20;


        /// <summary>
        /// Gets or sets the <see cref="ICommentRepository"/> instance.
        /// </summary>
        /// <value>The <see cref="ICommentRepository"/> instance.</value>
        public ICommentRepository CommentRepository { get; private set; }


        /// <summary>
        /// Initializes a new instance of the <see cref="CommentController"/> class.
        /// </summary>
        /// <param name="commentRepository">The <see cref="ICommentRepository"/> instance.</param>
        public CommentController(ICommentRepository commentRepository)
        {
            CommentRepository = commentRepository;
        }


        public ActionResult List(CommentStatus? commentStatus, int? pageNumber)
        {
            int pageIndex = (pageNumber ?? 1) - 1;
            ViewData["comments"] = CommentRepository.FindAllByStatus(commentStatus ?? CommentStatus.Normal, pageIndex, PageSize);
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            Comment comment = CommentRepository.Find(id);
            if (comment != null) {
                CommentRepository.Delete(comment);
            }

            if (Request.UrlReferrer != null) {
                return Redirect(Request.UrlReferrer.ToString());
            }

            return this.RedirectToAction(x => x.List(null, null));
        }
    }
}